(function (G) {
    const javaUrl = G.Storage.get('JAVAURL')
    G.vue({
        data: {
            listArr: [],
            isSelect: false,
            type: 1,
            resourceTypes:[],
            resourceTypeIndex:0
        },
        methods: {
            changeTab(event){
                this.setData({
                    resourceTypeIndex:event.detail.value
                })
                this.getList()
            },
            selectHandler(e) {
                const {
                    curindex,
                    index
                } = e.currentTarget.dataset
                const {
                    listArr
                } = this.data
                let list = JSON.parse(JSON.stringify(listArr))

                list[index].deductLevelList[curindex].isSelect = !list[index].deductLevelList[curindex].isSelect

             
                this.setData({
                    listArr: list,
                    isSelect: true
                })
            },
            getList() {
                let data = {
                    type:this.data.type
                }
                if(this.data.type == 1 || this.data.type == 2){
                    data.resourceType = this.data.resourceTypes[this.data.resourceTypeIndex].id
                }
                G.post(javaUrl + '/deducttype/list', data).then(rej => {
                    let listData = rej.data || []
                    listData.forEach(item => {
                        item.deductLevelList.forEach(itemTwo => {
                            itemTwo.isSelect = false
                        })
                    })
                    this.setData({
                        listArr: listData
                    })
                })
            },
            submit() {
                const {
                    isSelect,
                    type
                } = this.data
                if (!isSelect) {
                    wx.showToast({
                        title: '请选择',
                        icon: 'none'
                    })
                    return
                }
                const {
                    listArr
                } = this.data
                let selectList = []
                listArr.forEach(item => {
                    item.deductLevelList.forEach(itemTwo => {
                        if (itemTwo.isSelect) selectList.push(itemTwo)
                    })
                })
                let resourceTypeId = this.data.resourceTypes[this.data.resourceTypeIndex] ? this.data.resourceTypes[this.data.resourceTypeIndex].id : ''
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.emit('acceptDataFromOpenedPage', {
                    list: selectList,
                    type,
                    resourceTypeId
                });
                wx.navigateBack()

            }
        },
        mounted(opt) {
            this.setData({
                type: opt.type
            })
            if(opt.type == '1' || opt.type == '2'){
                G.get('/api/model/deductResourceType').then(res => {
                    let resourceTypeIndex = 0
                    if(opt.resourceTypeId){
                        for(let i = 0; i < res.length; i ++){
                            if(res[i].id == opt.resourceTypeId){
                                resourceTypeIndex = i
                                break
                            }
                        }
                    }
                    this.setData({
                        resourceTypeIndex,
                        resourceTypes:res
                    })
                    this.getList()
                })
            }else{
                this.getList()
            }
        }
    })
})(wx.$)